#include "GuiTest.h"


int main(int argc, char* argv[])
{
    GLEngine* engine = new GLEngine("GuiTest - F1 to load gui, F5 to refresh",
                                    800, 600, 32, false, 0, 8);
    engine->createClient<GuiTest>("guitest");
    engine->runClient("guitest");
    delete engine;
}


GuiTest::GuiTest(GLEngine* engine) :
    GLClient(engine)
{
    //get video modes
    GLFWvidmode modes[100];
    int n = glfwGetVideoModes(modes, 100);
    for (int i = 0; i < n; i++)
    {
        int w = modes[i].Width;
        int h = modes[i].Height;
        int bpp = modes[i].RedBits + modes[i].GreenBits + modes[i].BlueBits;
        if (bpp == 24)
        {
            vmodes_str += w;
            vmodes_str += " x ";
            vmodes_str += h;
            vmodes_str += '|';
            vmodes.push_back(VM(w, h));
        }
    }
    String cur_vmode;
    cur_vmode += getEngine()->getWidth();
    cur_vmode += " x ";
    cur_vmode += getEngine()->getHeight();
}

GuiTest::~GuiTest()
{
}

void GuiTest::onEnter()
{
}

void GuiTest::onLeave()
{
}

void GuiTest::onEvent(GLEvent e)
{
    switch (e.type)
    {
    case EVT_KeyPress:
        if (e.key.code == KEY_ESC)
        {
            getEngine()->quit(0);
        }
        else if (e.key.code == KEY_F1)
        {
            char fn[1024];
            if (show_open_box(fn, "XML Gui Files|*.xml"))
            {
                myLoadGui(fn);
            }
        }
        else if (e.key.code == KEY_F5)
        {
            myLoadGui(xmlFileName.c_str());
        }
        break;
    case EVT_GuiClick:
        switch (e.gui.id)
        {
        case 103:
            {
                showGuiMenu(0, e.gui.ctl->getHeight(), e.gui.ctl, vmodes_str.c_str(), 201);
                getGuiPopupMenu()->setSize(e.gui.ctl->getWidth(), 0);
            }
            break;
        }
        break;
    }
}

void GuiTest::onUpdate(float dt)
{
}

void GuiTest::onDraw()
{
    Color cm[4] = { 0xFF440000, 0xFF440000, 0xFFFF8800, 0xFFFF8800 };
    rectf r(0, 0, 800, 600);

    glBeginSceneGUI(800, 600);
    glFillRectCM(r, cm);
    glEndSceneGUI();
}

void GuiTest::myLoadGui(const char* fn)
{
    if (loadGuiXml(fn))
    {
        xmlFileName = fn;
        glfwSetWindowSize(getGuiDesktopControl()->getWidth(), getGuiDesktopControl()->getHeight());
    }
}





